﻿using System;
using System.Collections.Generic;
using System.Text;
using NUnit.Core;
using NUnit.Framework;
using System.Text.RegularExpressions;

namespace SpGetter
{
    [TestFixture]
    public class TestClass
    {
        private SpGetter GetSpGetter()
        {
            return new SpGetter("DEV-DB1", "kidbtest", "sa", "flvmf2007");
        }

        private string GetSPContentStringTrimSort(string content)
        {
            string[] empty_replaced_char = new string[]{"\r\n", "\t"};
            for (int i = 0; i < empty_replaced_char.Length; i++){
                content = content.Replace(empty_replaced_char[i], " ");
            }

            string[] splitted = content.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
            string ret = string.Empty;
            for (int i = 0; i < splitted.Length; i++)
            {
                if (splitted[i].Trim() != string.Empty)
                    ret += i != splitted.Length - 1 ? splitted[i].Trim() + " " : splitted[i].Trim();
            }
            return ret;
        }

        [Test]
        public void Test_connection_test()
        {
            SpGetter getter = new SpGetter("DEV-DB1", "Prima", "sa", "flvmf2007");
            Assert.IsTrue(getter.connection_test());

            getter = new SpGetter("DEV-DB1", "Prima", "pa", "flvmf2007");
            Assert.IsFalse(getter.connection_test());

            getter = new SpGetter("DEV-DB1", "Prima", "sa", "flvmf2006");
            Assert.IsFalse(getter.connection_test());
        }

        [Test]
        public void Test_get_procedure()
        {
            Procedure prc = new Procedure();
            prc.Content = @"CREATE PROCEDURE dbo.SelectAll AS Select * from Test";
            prc.Name = "SelectAll";
            Assert.AreEqual(prc.Name, GetSpGetter().get_procedure("SelectAll").Name);
            Assert.AreEqual(prc.Content, GetSpGetter().get_procedure("SelectAll").Content.Trim());
        }

        [Test]
        public void Test_GetParam()
        {
            string procedure = @"
  ALTER PROCEDURE dbo.SelectIndex 
	@index int
AS
	Select * from Test where idx = @index
                RETURN
";
            string ordered_procedure = "ALTER PROCEDURE dbo.SelectIndex @index int AS Select * from Test where idx = @index RETURN";
            string tmp = GetSPContentStringTrimSort(procedure);
            Assert.AreEqual(ordered_procedure, tmp);
        }

        [Test]
        public void Test_ParamParse()
        {
            string ordered_procedure = "ALTER PROCEDURE dbo.SelectIndex @index int AS Select * from Test where idx = @index RETURN";
            string splited_under_as = Regex.Split(ordered_procedure, "AS")[0];
            MatchCollection col = Regex.Matches(splited_under_as, "@[a-zA-Z]* [a-zA-Z0-9\\(\\)]*");

            if(col.Count > 0)
            {
                foreach(Capture c in col[0].Captures)
                {
                    // @index int
                    string param_name = Regex.Match(c.Value, "@[a-zA-Z]+").Value;
                }
            }
        }

    }
}